from django.shortcuts import render_to_response, HttpResponseRedirect, Http404
from django.template import RequestContext
from django.contrib import messages

from apps.course.functions import course_reverse
from apps.management import Management
from models import *

import logging

logger = logging.getLogger('pet.apps.additions.manage')

def manage(request, TheCourseParticipant, TheCourse):
    Additions = []
    for model in sorted(ModelAdditionList):
        ModelName = unicode(model._meta.verbose_name_plural)
        Additions.append((ModelName,ModelAddition.find(TheCourse.course_id,model=model.__name__)))

    logger.debug("Additions - %s",Additions)
    return render_to_response('additions/manage.html',{'Additions':Additions},context_instance=RequestContext(request))
#Management.register_interface('Additions','ASSISTANT;INSTRUCTOR','manage',manage)
    
def edit(request, TheCourseParticipant, TheCourse, model=None, name=None):
    TheAddition = None
    if model and name: TheAddition = ModelAddition.find(TheCourse.course_id, model, name)
    
    if request.method == 'POST':
        AdditionForm = ModelAdditionForm(request.POST, instance=TheAddition, course=course)
        if AdditionForm.is_valid():
            if AdditionForm.has_changed():
                AdditionForm.save()
            else:
                logger.form_unchanged("Addition Form unchanged")
            if 'Save' in request.POST and request.POST['Save'] == 'SaveReturn':
                return HttpResponseRedirect(course_reverse('management',TheCourse.course_id,app='additions'))
        else:
            logger.debug("Addition Form invalid")
    else:
        AdditionForm = ModelAdditionForm(instance=TheAddition,course=TheCourse.course_id)
        
    return render_to_response('additions/edit.html',{'form':AdditionForm,'TheAddition':TheAddition},context_instance=RequestContext(request))
#Management.register_interface('Additions','ASSISTANT;INSTRUCTOR','edit',edit)

def remove(request, TheCourseParticipant, TheCourse, model, name):
    TheObject = ModelAddition.find_or_404(TheCourse.course_id, model, name)
    
    if TheCourseParticipant.status != "INSTRUCTOR" and TheCourseParticipant.status != "ASSISTANT":
        raise Http404
    Result = remove_course(TheObject, TheCourse.course_id)
    if (Result != "None"):
        messages.info(request,"Addition %s %s" % (TheObject, Result))
    return manage(request, TheCourseParticipant, TheCourse.course_id)
#Management.register_interface('Additions','ASSISTANT;INSTRUCTOR','remove',remove)

def delete(request, TheCourseParticipant, TheCourse, model=None, name=None):
    TheAddition = None
    TheAddition = ModelAddition.find_or_404(TheCourse.course_id, model, name)
    
    if request.method == "POST" and 'DeleteConfirmation' in request.POST:
        #Remove this addition from this course
        if len(TheAddition.course.split(',')) == 1:
            TheAddition.delete()
            messages.info(request,"Addition successfully deleted")
        else:
            Courses = TheAddition.course.split(',')
            Courses.remove(TheCourse.course_id)
            TheAddition.course = ','.join(courses)
            TheAddition.save()
            messages.info(request,"Addition removed from course %s" % TheCourse.course_id)
        return HttpResponseRedirect(course_reverse('management',TheCourse.course_id,app='additions'))

    # Render a form otherwise
    else:
        return render_to_response('additions/delete.html',
            {'TheAddition': TheAddition},
            context_instance=RequestContext(request)
        )
#Management.register_interface('Additions','ASSISTANT;INSTRUCTOR','delete',delete)

